// pages/truckCenter/detail.js
import requestUtil from '../../baseRequest'
import requestUrls from '../../requestUrl'
import commonUtils from '../../utils/util.js'

let openid = getApp().getUserOpenid()
let DEFAULT_PAGE = 1
let DEFAULT_SIZE = 10
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userType: '',
    pageName: '',
    fromPage: '',
    openId: openid,
    truckCenterId: '',
    publishSide: '',
    truckCenter: {},
    truckCenterImageList: [],
    mainDriverId: {},
    mainDriverLicense: {},
    orderCenterList: [],
    showBtn: '',
    baseImagePrefixUrl: requestUrls.baseImagePrefixUrl,
  },
  //货主方申请车辆
  linkToAddOrderCenter: function () {
    let addOrderCenter = {
      truckCenterId: this.data.truckCenterId,
      publishSide: this.data.publishSide,
    }
    wx.setStorageSync('addOrderCenter', addOrderCenter);
    wx.navigateTo({
      url: '/pages/orderCenter/addv2'
    })
  },
  //查询运输单数据
  getById() {
    let formData = {};
    if (this.data.otiId) {
      formData = {
        openId: this.data.openId,
        criteria: {
          id: this.data.otiId,
        }
      }
    } else {
      formData = {
        openId: this.data.openId,
        criteria: {
          truckCenter: this.data.orderCenterId ? {} : {
            id: this.data.truckCenterId,
          },
          orderCenterId: this.data.orderCenterId,
          publishSide: this.data.publishSide,
        }
      }
    }
    requestUtil.postRequest(requestUrls.truckOrder.getById, formData, (res) => {
      if (!res.hasError) {
        console.log(res);
        const { truckCenter = {}, orderCenter = {}, id } = res.result;
        const { orderTruckImageList = [], } = truckCenter;
        if (orderTruckImageList && orderTruckImageList.length > 0) {
          for (let i = 0; i < orderTruckImageList.length; i++) {
            if (orderTruckImageList[i].imageTypeId === 'LICENSE_IMAGE') {
              this.setData({
                mainDriverId: {
                  ...orderTruckImageList[i],
                  imageUrl: requestUrls.baseImagePrefixUrl + orderTruckImageList[i].imageUrl,
                }
              })
            }
            if (orderTruckImageList[i].imageTypeId === 'VEHICLE_LICENSE') {
              this.setData({
                mainDriverLicense: {
                  ...orderTruckImageList[i],
                  imageUrl: requestUrls.baseImagePrefixUrl + orderTruckImageList[i].imageUrl,
                }
              })
            }
          }
        }
        let showBtn = '';
        let statusName = '';
        if (this.data.publishSide === 'ORDER_CENTER') {
          //查看方为货主，不存在委托单，则展示按钮：申请托运
          if (!orderCenter) {
            showBtn = 'sqty';
          } else {
            //存在委托单
            //委托单状态为已发布，则展示按钮：合作拒绝
            if (orderCenter.statusId === '20') {
              showBtn = 'hzjj';
              statusName = '待确认';
            }
            //且单状态为申请中，则展示按钮：取消申请
            if (orderCenter.statusId === '30') {
              showBtn = 'qxsq';
              statusName = '申请中';
            }
            //状态为35，不显示按钮
            if (orderCenter.statusId === '35' || orderCenter.statusId === '40') {
              showBtn = 'odzh';
              statusName = orderCenter.statusName;
            }
            //状态为60，已送达，显示按钮确认收货
            if (orderCenter.statusId === '60') {
              showBtn = 'oysd';
              statusName = orderCenter.statusName;
            }
          }

        } else if (this.data.publishSide === 'TRUCK_CENTER') {
          //查看方为车主
          //状态为已发布,显示按钮：取消发布
          if (truckCenter.statusId === '20') {
            if (!orderCenter) {
              showBtn = 'qxfb';
            } else {
              showBtn = 'sqz';
            }
          }
          if (truckCenter.statusId === '30' || truckCenter.statusId === '40' || truckCenter.statusId === '60' || truckCenter.statusId === '35') {
            showBtn = 'sqz';
          }



        }
        this.setData({
          showBtn,
          statusName,
        })

        if (orderCenter) {
          this.setData({
            orderCenter: {
              ...orderCenter,
              loadingBeginTime: commonUtils.formatDate(orderCenter.loadingBeginTime, 'MM-dd hh'),
              loadingEndTime: commonUtils.formatDate(orderCenter.loadingEndTime, 'MM-dd hh'),
            },
          })
        }


        this.setData({
          orderTruckInfoId: id,
          truckCenter: {
            ...truckCenter,
            freeBeginTime: commonUtils.formatDate(truckCenter.freeBeginTime, 'MM-dd hh'),
            freeEndTime: commonUtils.formatDate(truckCenter.freeEndTime, 'MM-dd hh'),
          },
        })
      }
    })
  },
  cancelApplyOrderCenter: function () {
    wx.navigateTo({
      url: '/pages/orderCenter/cancelApply?publishSide=ORDER_CENTER&orderCenterId=' + this.data.orderCenter.id,
    })
    // const formData = {
    //   openId:this.data.openId,
    //   criteria:{
    //     publishSide : this.data.publishSide,
    //     orderCenterId:this.data.orderCenterId,
    //   }
    // }
    // requestUtil.postRequest(requestUrls.truckOrder.cancelApply, formData, (res) => {
    //   if(!res.hasError){
    //     wx.showModal({
    //       title: '温馨提示',
    //       content: '操作成功',
    //       showCancel: false,
    //       success: res => {
    //         wx.switchTab({
    //           url:'/pages/homev2/index'
    //         })
    //       }
    //   })
    //   }
    // })
  },
  /**
   * 车主方同意合作
   */
  dealApply: function (event) {
    const optName = event.currentTarget.dataset.opt;
    const formData = {
      openId: this.data.openId,
      criteria: {
        orderCenterId: this.data.orderCenter.id,
        truckCenterId: this.data.truckCenter.id,
        //同意合作：传值Y;拒绝合作：传值N
        publishSide: this.data.publishSide || 'TRUCK_CENTER',
        statusName: optName
      }
    }
    requestUtil.postRequest(requestUrls.truckOrder.dealOrReject, formData, (res) => {
      if (!res.hasError) {
        wx.showModal({
          title: '温馨提示',
          content: '操作成功',
          showCancel: false,
          success: res => {
            wx.switchTab({
              url: '/pages/homev2/index'
            })
          }
        })
      }
    })
  },
  confirmReceive: function (event) {
    const truckCenterId = this.data.truckCenter.id;
    const orderCenterId = this.data.orderCenter.id;
    const formData = {
      openId: this.data.openId,
      criteria: {
        orderCenterId,
        truckCenterId,
      }
    }
    requestUtil.postRequest(requestUrls.orderCenter.confirmReceive, formData, (res) => {
      if (!res.hasError) {
        wx.showModal({
          title: '温馨提示',
          content: '操作成功',
          showCancel: false,
          success: res => {
            wx.switchTab({
              url: '/pages/homev2/index'
            })
          }
        })
      }
    })
  },
  makePhoneCall(event) {
    const phoneNumber = event.currentTarget.dataset.phonenumber;
    commonUtils.callSomeBody(phoneNumber);
  },
  showOrderInfo(event) {
    console.log(this.data);
    wx.navigateTo({
      url: '/pages/orderCenter/detail?publishSide=ORDER_CENTER&otiId=' + this.data.orderTruckInfoId
    })
  },
  //点击货源信息，跳转到货源页面
  showDetail: function (event) {
    const id = event.currentTarget.dataset.ordercenterid;
    wx.navigateTo({
      url: '/pages/orderCenter/detail?orderCenterId=' + id + '&publishSide=' + this.data.publishSide
    })

  },
  previewImage(event) {
    const imageUrl = event.currentTarget.dataset.imgurl;
    wx.previewImage({
      urls: [imageUrl],
    })

  },
  //取消发布
  cancelOrder() {
    const formData = {
      openId: this.data.openId,
      criteria: {
        truckCenterId: this.data.truckCenterId,
        //同意合作：传值Y;拒绝合作：传值N
        publishSide: this.data.publishSide || 'TRUCK_CENTER',
      }
    }
    requestUtil.postRequest(requestUrls.truckOrder.cancelOrder, formData, (res) => {
      if (!res.hasError) {
        wx.showModal({
          title: '温馨提示',
          content: '操作成功',
          showCancel: false,
          success: res => {
            wx.switchTab({
              url: '/pages/homev2/index'
            })
          }
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("车辆详情页面传入参数：");
    console.log(options);
    const { truckCenterId = '', publishSide = '', orderCenterId = '', otiId = '' } = options;
    this.setData({
      truckCenterId: truckCenterId,
      publishSide: publishSide,
      orderCenterId: orderCenterId,
      otiId,
    })
    this.getById();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/homev2/index'
    }
  }
})